import { Body, Controller, Get, Post, Res, UseInterceptors, Request, Query, HttpException, HttpStatus } from '@nestjs/common';
import { ValidTokenInterceptor } from 'src/interceptor/validToken';
import { ValidRSAInterceptor } from 'src/interceptor/validRSA';
import { ConcernService } from './concern.service';

import {
  apibefore
} from '../../router'
import { Concern } from './concern.entity';
import utils from 'src/common/utils';
import { RedisCacheService } from '../redisCache/redisCache.service';
@Controller(apibefore('/concern'))
@UseInterceptors(ValidTokenInterceptor)
@UseInterceptors(ValidRSAInterceptor)
export class ConcernController {
  constructor(
    private readonly service: ConcernService,
    private readonly redisCacheService: RedisCacheService,
  ) {}

  @Post('/create')
  async create(@Body() data: Concern, @Request() req) {
    let concern = new Concern()
    concern.concern_wx_user_id = data.concern_wx_user_id || undefined
    concern.concern_ic_group = data.concern_ic_group || undefined
    concern.create_time = new Date()
    concern.wx_user = req.headers.wx_user
    
    let result = await this.service.create(concern)
    if(result.error){
      return new HttpException(result.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }else{
      return result
    }
  }

  @Post('/create_by_video_id')
  async create_by_video_id(@Body() data, @Request() req){
    return await this.service.create_by_video_id(data.video_id, req.headers.wx_user)
  }

  @Post('/delete')
  async delete(@Body() data: {id: number, unconcern: string, type: string, ic_info_id: number}, @Request() req) {
    let err = await this.service.remove(data.id, data.unconcern, req.headers.wx_user, data.type, data.ic_info_id)
    if(err){
      return new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR)
    }else{
      return true
    }
  }

  @Get('/findAll_My')
  async findAll_My(@Query() params,@Request() req){
    let wx_user: string = req.headers.wx_user
    let keyword = params.keyword
    if(!utils.validIsNull(keyword)){
      return this.service.findAll_ByWx_User(req.headers.wx_user, params.keyword)
    }else{
      let cacheKey = `${wx_user}:concern_list`
      let res: any = await this.redisCacheService.get(cacheKey)
      if(res){
        await Promise.all(res.map(async e => {
          await Promise.all(e.data.map(async (c,index) => {
            if(c.id && !c.unconcern_id){
              let cacheKeyIcInfo = `icinfo:icgroup:${c.id}:info`
              let info:any = await this.redisCacheService.get(cacheKeyIcInfo)
              if(!info){
                let dataInfo = await this.service.findIcInfo(wx_user, undefined, c.id)
                if(dataInfo){
                  await this.redisCacheService.set(cacheKeyIcInfo, dataInfo)
                }
              }
              info = await this.redisCacheService.get(cacheKeyIcInfo)
              let cacheKeyPlatform = `icinfo:icgroup:${c.id}:platform`
              let platform: any = await this.redisCacheService.get(cacheKeyPlatform)
              if(!platform){
                let dataPL = await this.service.findIcPlatform(c.id, undefined)
                if(info){
                  info.platform_count = dataPL.length
                  await this.redisCacheService.set(cacheKeyIcInfo, info)
                }
                await this.redisCacheService.set(cacheKeyPlatform, dataPL)
              }
              info = await this.redisCacheService.get(cacheKeyIcInfo)
              platform = await this.redisCacheService.get(cacheKeyPlatform)
              let d = {
                id: c.id,
                concern_id: c.concern_id,
                ...info,
                platformList: platform
              }
              e.data[index] = d
            }else if(c.wx_user_id){
              let cacheKeyIcInfo = `icinfo:wxuser:${c.wx_user_id}:info`
              let info:any = await this.redisCacheService.get(cacheKeyIcInfo)
              if(!info){
                let dataInfo = await this.service.findIcInfo(wx_user, c.wx_user_id)
                if(dataInfo){
                  await this.redisCacheService.set(cacheKeyIcInfo, dataInfo)
                }
              }
              info = await this.redisCacheService.get(cacheKeyIcInfo)
              let cacheKeyPlatform = `icinfo:wxuser:${c.wx_user_id}:platform`
              let platform: any = await this.redisCacheService.get(cacheKeyPlatform)
              if(!platform){
                let dataPL = await this.service.findIcPlatform(undefined, c.wx_user_id)
                if(info){
                  info.platform_count = dataPL.length
                  await this.redisCacheService.set(cacheKeyIcInfo, info)
                }
                await this.redisCacheService.set(cacheKeyPlatform, dataPL)
              }
              info = await this.redisCacheService.get(cacheKeyIcInfo)
              platform = await this.redisCacheService.get(cacheKeyPlatform)
              let d = {
                wx_user_id: c.wx_user_id,
                concern_id: c.concern_id,
                ...info,
                platformList: platform
              }
              e.data[index] = d
            }else if(c.unconcern_id){
              e.data[index] = c
            }
          }))
        }))
        return res
      }else{
        let data: any = await this.service.findAll_ByWx_User(req.headers.wx_user, params.keyword)
        data = data.data
        let newData = []
        data.map((e: any) => {
          let child = []
          let item = {
            key: e.key,
            data: child
          }
          e.data.map(c => {
            if(c.id && !c.unconcern_id){
              child.push({id: c.id, concern_id: c.concern_id})
            }else if(c.wx_user_id){
              child.push({wx_user_id: c.wx_user_id, concern_id: c.concern_id})
            }else if(c.unconcern_id){
              child.push(c)
            }
          })
          newData.push(item)
        })
        await this.redisCacheService.set(cacheKey, newData)
        return data
      }
    }
  }

  @Get('/findAll_Works')
  async findAll_Works(@Query() params, @Request() req){
    let { skip, take } = utils.getLimit(params.pageSize, params.pageIndex)
    return await this.service.findAll_Works(skip, take, req.headers.wx_user, params.clearConcern == 1)
  }

  @Get('/findIsUpdate')
  async findIsUpdate(@Query() params, @Request() req){
    return await this.service.findIsUpdate(req.headers.wx_user)
  }

  @Get('/findConcern_by_video_id')
  async findConcern_by_video_id(@Query() params, @Request() req){
    return await this.service.findConcern_by_video_id(params.video_id, req.headers.wx_user)
  }
}
